package com.k8.rpc.expoter;

import com.k8.common.event.Event;
import com.k8.common.event.Listener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 负责将内部保存导出的Exporter执行register
 * 例如配置中心发生变化，或者其它外部条件发生变化
 */
@Slf4j
public class ExporterListener implements Listener {


    @Override
    public boolean judgeEventType(Event event) {
        if (event instanceof ExporterEvent) return true;
        return false;
    }

    //ExporterListener不需要特别复杂的条件验证，judgeEventType可以满足验证条件
    @Override
    public boolean canDispose(Event event) {
        return event instanceof ExporterEvent;
    }

    @Override
    public void disposal(Event event) {
        ExporterEvent exporterEvent = (ExporterEvent) event;
        Throwable exception = event.getException();
        if (exception == null) {
            log.info("上传配置成功：registerPath: %s, protocolPath: %s", exporterEvent.getRegisterPath(), exporterEvent.getProtocolPath());
        } else {
            log.info("上传配置失败：registerPath: %s, protocolPath: %s", exporterEvent.getRegisterPath(), exporterEvent.getProtocolPath());
        }
    }

    public static class ExporterEvent implements Event {
        private Throwable throwable;
        private String registerPath;
        private String protocolPath;

        public ExporterEvent(String registerPath, String protocolPath, Throwable throwable) {
            this.throwable = throwable;
            this.registerPath = registerPath;
            this.protocolPath = protocolPath;
        }

        @Override
        public Throwable getException() {
            return this.throwable;
        }

        @Override
        public Object getValue() {
            return null;
        }

        public String getRegisterPath() {
            return registerPath;
        }

        public String getProtocolPath() {
            return protocolPath;
        }
    }
}
